package com.cloudputing.hbase.original.wiki.dao;

import java.io.IOException;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.mortbay.log.Log;

import com.cloudputing.hbase.spring.wiki.valueobject.Wiki;

public class WikiOriDao {
	
	private Configuration myConf = HBaseConfiguration.create();
	private HTablePool pool = new HTablePool(myConf,3);
	private HTableInterface wikiTable = null;
	
	private String tableName = "wiki";
	private byte[] familyName = Bytes.toBytes("text");
	private byte[] qName = Bytes.toBytes("qual1");
	private byte[] qPhone = Bytes.toBytes("qual2");
	
	public List<Wiki> findAll() throws IOException
    {
    	try {
			wikiTable = pool.getTable(tableName);
			ResultScanner resultScanner = wikiTable.getScanner(new Scan());
			for(Result result : resultScanner)
			{
				Log.info(result.toString());
			}
			return null;
		} finally {
			try {
				wikiTable.close();
				pool.closeTablePool(tableName);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    }
	
	public void saveWiki(Wiki wiki) throws IOException
    {
		try {
			wikiTable = pool.getTable(tableName);
			Put put = new Put(Bytes.toBytes(wiki.getRowId()));
			put.add(familyName, qName,Bytes.toBytes(wiki.getName()));
			put.add(familyName, qPhone,Bytes.toBytes(wiki.getPhone()));
			wikiTable.put(put);
		} finally {
			try {
				wikiTable.close();
				pool.closeTablePool(tableName);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    }

}
